---
title: "COVID-19 Evacuation Study"
subtitle: "Modeling Code"
authors: "Courtney Page-Tan and Timothy Fraser"
output: html_notebook
---

This study tests whether evacuation from severe disasters led to upticks in the spread of COVID. We estimate evacuation using data from Facebook Data for Good. Creation of the dataset is described in 'replication_code.Rmd."

This document directly follows 'replication_code.Rmd' and provides code for generating model tables.



# 4. Matching

Next, this study conducts two matching experiments on each of our samples.

To get our sample, we will zoom into the start of each crisis (for the Glass Fire, September 27, and for Hurricane Zeta, October 28).

1) We will identify communities with similar levels of X, Y, and Z except that some were heavily damaged while others were not.

2) Zooming into our affected region, we will identify communities with similar levels of X, Y, and Z except that some had high evacuation while others did not.

```{r}
library(MatchIt)
library(cem)
library(tidyverse)

# Let's identify several county subdivisions as similar as possible except that (1) they were affected by the disaster versus not, and then (2) they were affected by evacuation versus not.

#################################
# 1. Match by Disaster Damage
#################################

# Let's zoom into the disaster affected region and identify places which experienced MORE vs. LESS rain

# Some parts of the affected study area were deeply affected by the storm, while others were not. For example, Plaquemines, Terrebonne, Lafourche, Jefferson, and Orleans Parishes all ordered voluntary or mandatory evacuation orders, but 16 other parishes did not. Rainfall data indicates the same.

# So, from within this affected area, let's identify some communities which experienced less rainfall than others.
dat <- read_rds("raw_data/la_dataset.rds") %>%
  # Since each rainfall stat is lagged by 2 weeks,
  # we must zoom into TWO WEEKS AFTER THE WEEK OF THE DISASTER,
  # switching from the start of the disaster in "2020-10-28" to "2020-11-11"
  # Zooming into the  week of the disaster,
  filter(week == "2020-11-11") %>%
  # Let's identify those above and below the median
  mutate(treat = ntile(avg_rainfall, 2) - 1)

# Now, let's identify communities as similar as possible
# in terms of key determinants of disaster damage
m1 <- dat %>%
  matchit(formula = treat ~ pop_density + social_capital + svi +
            employee_muni_int, method = "cem")

dat %>%
  mutate(weights = m1$weights) %>%
  filter(weights > 0) %>%
  select(csub, weights) %>%
  saveRDS("raw_data/la_disaster_match.rds")


# Now, let's repeat for California
dat <- read_rds("raw_data/ca_dataset.rds") %>%
  # Zoom into the week of the disaster,
  # Since active_fires is lagged by 2 weeks, we need to zoom into 2 weeks AFTER
  # the date of the disaster ("2020-09-28"), switching to "2020-10-12"
  filter(week == "2020-10-12")

# Now, let's identify communities as similar as possible
# in terms of key determinants of disaster damage
m2 <- dat %>%
  # Couldn't include government capacity; had to remove
  matchit(formula = active_fire ~ pop_density + social_capital + svi +
            employee_muni_int,
          method = "cem")

dat %>%
  mutate(weights = m2$weights) %>%
  filter(weights > 0) %>%
  select(csub, weights) %>%
  saveRDS("raw_data/ca_disaster_match.rds")

#################################
# 2. Match by MORE Evacuation!
#################################

# So, from within this affected area, let's identify some communities which experienced less rainfall than others.
dat <- read_rds("raw_data/la_dataset.rds") %>%
   # Since each rainfall stat is lagged by 2 weeks,
  # we must zoom into TWO WEEKS AFTER THE WEEK OF THE DISASTER,
  # switching from the start of the disaster in "2020-10-28" to "2020-11-11"
  # Zooming into the  week of the disaster,
  filter(week == "2020-11-11") %>%
   # Let's identify those with increased evacuation above and below the median
  mutate(treat = if_else(evacuation_more > 0, 1, 0))

# Now, let's identify communities as similar as possible
# in terms of key determinants of disaster damage
m3 <- dat %>%
  matchit(formula = treat ~ pop_density + social_capital + svi +
            employee_muni_int, method = "cem")

dat %>%
  mutate(weights = m3$weights) %>%
  filter(weights > 0) %>%
  select(csub, weights) %>%
  saveRDS("raw_data/la_evac_match.rds")



# So, from within this affected area, let's identify some communities which experienced less rainfall than others.
dat <- read_rds("raw_data/ca_dataset.rds") %>%
   # Zoom into the week of the disaster,
  # Since active_fires is lagged by 2 weeks, we need to zoom into 2 weeks AFTER
  # the date of the disaster ("2020-09-28"), switching to "2020-10-12"
  filter(week == "2020-10-12") %>%
  # Let's identify those with increased evacuation above and below the median
  mutate(treat = if_else(evacuation_more > 0, 1, 0))

# Now, let's identify communities as similar as possible
# in terms of key determinants of disaster damage
m4 <- dat %>%
  matchit(formula = treat ~ pop_density + social_capital + svi +
            employee_muni_int, method = "cem")

dat %>%
  mutate(weights = m4$weights) %>%
  filter(weights > 0) %>%
  select(csub, weights) %>%
  saveRDS("raw_data/ca_evac_match.rds")

remove(dat)


#################################
# 3. Match by LESS Evacuation!
#################################

# So, from within this affected area, let's identify some communities which experienced less rainfall than others.
dat <- read_rds("raw_data/la_dataset.rds") %>%
   # Since each rainfall stat is lagged by 2 weeks,
  # we must zoom into TWO WEEKS AFTER THE WEEK OF THE DISASTER,
  # switching from the start of the disaster in "2020-10-28" to "2020-11-11"
  # Zooming into the  week of the disaster,
  filter(week == "2020-11-11") %>%
   # Let's identify those with DECREASED evacuation above and below the median
  mutate(treat = if_else(evacuation_less > 0, 1, 0))

# Now, let's identify communities as similar as possible
# in terms of key determinants of disaster damage
m5 <- dat %>%
  matchit(formula = treat ~ pop_density + social_capital + svi +
            employee_muni_int, method = "cem")

dat %>%
  mutate(weights = m5$weights) %>%
  filter(weights > 0) %>%
  select(csub, weights) %>%
  saveRDS("raw_data/la_shelter_match.rds")



# So, from within this affected area, let's identify some communities which experienced less rainfall than others.
dat <- read_rds("raw_data/ca_dataset.rds") %>%
   # Zoom into the week of the disaster,
  # Since active_fires is lagged by 2 weeks, we need to zoom into 2 weeks AFTER
  # the date of the disaster ("2020-09-28"), switching to "2020-10-12"
  filter(week == "2020-10-12") %>%
  # Let's identify those with DECREASED evacuation above and below the median
  mutate(treat = if_else(evacuation_less > 0, 1, 0))

# Now, let's identify communities as similar as possible
# in terms of key determinants of disaster damage
m6 <- dat %>%
  matchit(formula = treat ~ pop_density + social_capital + svi +
            employee_muni_int, method = "cem")

dat %>%
  mutate(weights = m6$weights) %>%
  filter(weights > 0) %>%
  select(csub, weights) %>%
  saveRDS("raw_data/ca_shelter_match.rds")

remove(dat)


# Let's visualize the balance in these matched samples
# First, let's write a quick function to extract the absolute standardized mean differences
compare = function(matchmodel){
  bind_rows(
    # Grab the ASMD from the original data
    summary(matchmodel)[[3]] %>%
      as.data.frame() %>%
      tibble::rownames_to_column(var = "term") %>%
      select(term, std_mean_diff = 4) %>%
      mutate(sample = "Original"),
    # Grab the ASMD from the matched data
    summary(matchmodel)[[4]] %>%
      as.data.frame() %>%
      tibble::rownames_to_column(var = "term") %>%
      select(term, std_mean_diff = 4) %>%
      mutate(sample = "Matched")) %>%
    return()
}

out <- list(m1,m2,m3,m4,m5,m6) %>%
  map_dfr(~compare(.), .id = "model") %>%
  mutate(treatment = case_when(
    model %in% c(1,2) ~ "Disaster Damage\n(Active Fires\nor Rain over median)",     
    model %in% c(3,4) ~ "Evacuation\n(Any Increased\nMovement)",
    model %in% c(5,6) ~ "Shelter-in-Place\n(Any Decreased\nMovement)"),
    case = if_else(model %in% c(1,3,5), "Hurricane Zeta\nin Louisiana", 
                   "Glass Fire\nin California"),
    term = term %>% recode_factor(
      "pop_density" = "Population\nDensity",
      "social_capital" = "Social\nCapital",
      "svi" = "Social\nVulnerability",
      "employee_muni_int" = "Governance\nCapacity")) 
out %>% 
  ggplot(mapping = aes(x = term, y = abs(std_mean_diff), 
                       color = sample, group = sample)) +
  facet_grid(treatment ~ case, scales = "free") +
  geom_hline(yintercept = 0.1, color = "black", linetype = "dashed") +
  geom_line() +
  geom_point(size = 3) +
  geom_point(color = "white", alpha = 0.5) +
  theme_minimal(base_size = 14) +
  theme(panel.grid.minor = element_blank(),
        panel.border = element_rect(fill = NA, color = "black"),
        axis.text.x = element_text(hjust = 1, angle = 30),
        legend.position = "bottom",
        panel.spacing = unit(0.25, "cm")) +
  scale_color_manual(values = c("#785EF0", "#FFB000")) +
  labs(x = "Matching Covariates", y = "Absolute Standardized Mean Differences\nbetween Matching Covariate\nin Treatment vs. Control Group", color = "Sample") +
  ggsave("viz/matching_balance.png", dpi = 500, width = 8, height = 7)

rm(list = ls())
```





# 5. Analysis

## 5.1: 2 Weeks Later (Main Analysis)

### Random Effects

```{r}
# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate")), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int, weathertrans),
    funs(scale(.)))

# We almost definitely want to control for time
# It's not really appropriate to control for each municipality with a separate intercept
# especially because after all our covariates, we don't leave much variance there.


m1a <- la %>%
  lm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int + factor(week))

m1b <- la %>%
  lm(formula = positivity_rate ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int + factor(week))

rm(m1a,m1b)
# Note: although LA's positivity rates are surprisingly normally distributed for a rate,
# their residuals are not; log-transforming the positivity rates greatly improves the normality of residuals, reducing heteroskedasticity.

#ggplot(mapping = aes(x = m1a$residuals)) + 
#  geom_histogram()

#ggplot(mapping = aes(x = m1b$residuals)) + 
#  geom_histogram()

# Likewise, a likelihood ratio test shows a statistically significant improvement in the log-likelihood when we switch to log-transformation

#lmtest::lrtest(m1b, m1a)
# remove(m1b,m1a)

library(plm)
#la %>%
#  lm(formula = log(positivity_rate) ~ 
#        positivity_rate_lag2 + weathertrans +
#         evacuation_more + 
#         avg_rainfall +
#         bonding + bridging + linking + 
#         svi_socioeconomic + svi_household_disability + 
#         svi_minority + svi_housing_tranport +
#         workplaces_int + health_care_capacity_int + health_quality_int + 
#         employee_muni_int + democrat_2016_int + factor(week)) %>%
#  car::vif() %>%
#  .^2
# weathertrans is super colinearity with week


m1 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"))


m2 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"))

m3 <- la %>%
  plm(formula = log(positivity_rate) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"))


m4 <- la %>%
  plm(formula = log(positivity_rate) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"))

m5 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))

m6 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int, weathertrans),
      funs(scale(.)))

ca %>%
  lm(formula = log(case_rate) ~ 
        case_rate_lag2 + weathertrans +
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int + factor(week)) %>%
  car::vif() %>%
  .^2

m7 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))


m8 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m9 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))

phtest(m7,m8)

texreg::htmlreg(
  list(m1,#m2, 
       m3,#m4,
       m5,#m6,
       m7,#m8,
       m9,#m10,
       m11#,m12
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i>",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 3,344)<br>before and after Hurricane Zeta<br>in Southeastern Louisiana<br>among 176 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents<br>(n = 3,439) before and after the Glass Fire<br>in Central California<br>among 181 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A1-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A1-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A1-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A1-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A1-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A1-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity in California models, we split up socioeconomic status, minority status, health care capacity, and health care quality indices into four quantiles each. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 7.7.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE, single.row = TRUE,
#  custom.gof.names = c(
#    rep(NA, 3),
#    "s_idios" = "Subdivision Variance", 
#    "s_time" = "Temporal Variance")
)

remove(m1,m2,m3,m4,m5,m6,
       m7,m8,m9,m10,m11,m12)
```

### Matched by Disaster

```{r}


# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate")), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int, weathertrans),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_disaster_match.rds")) %>%
  filter(weights > 0)

# Check that colinearity is below 10 for all
#la %>%
#  lm(formula = log(positivity_rate) ~ 
#        positivity_rate_lag2 + weathertrans +
#         evacuation_more + 
#         avg_rainfall +
#         bonding + bridging + linking + 
#         svi_socioeconomic + svi_household_disability + 
#         svi_minority + svi_housing_tranport +
#        workplaces_int + health_care_capacity_int + health_quality_int + 
#         employee_muni_int + democrat_2016_int +
#        factor(week), weights = weights) %>%
#  car::vif() %>%
#  .^2
# Produces enormous colinearity between weathertrans (135) avg_rainfall (9.4) and week
# Can't control for weathertrans

library(plm)
m1 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)


m3 <- la %>%
  plm(formula = log(positivity_rate) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(positivity_rate) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = svi_minority,
         bonding = bonding) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int, weathertrans),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_disaster_match.rds")) %>%
  filter(weights > 0)



# In our matched models in California, socioeconomic status was strongly correlated with minority status, health care capacity, and health care quality. To reduce this colinearity, we had to remove minority status, health care capacity, and health care quality from models. Since they are highly correlated, their omission is not problematic, as they would be explaining the same variation anyway. 


ca %>%
  lm(formula = log(case_rate) ~ 
        case_rate_lag2 + #weathertrans +
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int + factor(week),
     weights = weights) %>%
  car::vif() %>%
  .^2
# Produces really high colinearity (14.8) with weathertrans

m7 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

phtest(m7,m8)

m9 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


texreg::htmlreg(
  list(m1,#m2,
       m3,#m4,
       m5,#m6,
       m7,#m8,
       m9,#m10,
       m11#m12
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_disaster.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Disaster Damage",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 608)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 32 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 437)<br>before and after the Glass Fire in Central California<br>among 23 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A2-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A2-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A2-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A2-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A2-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A2-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status, health care capacity, and health care quality indices, which were extremely correlated with socioeconomic status and bonding social capital. Socioeconomic status also was split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 7.5.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE
#   custom.gof.names = c(
#    rep(NA, 3),
#    "s_idios" = "Subdivision Variance", 
#    "s_time" = "Temporal Variance")
)

#remove(m1,m2,m3,m4,m5,m6,
#       m7,m8,m9,m10,m11,m12)
```


### Matched by Evacuation

```{r}
library(lme4)
library(lmerTest)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate")), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_evac_match.rds")) %>%
  filter(weights > 0)

library(plm)
m1 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m3 <- la %>%
  plm(formula = log(positivity_rate) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(positivity_rate) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_evac_match.rds")) %>%
  filter(weights > 0)


m7 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m9 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


texreg::htmlreg(
  list(m1,#m2,
       m3,# m4,
       m5,#m6,
       m8,#m7,
       m10,#m9,
       m12#m11
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_evac.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Evacuation",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1,026)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 54 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 969)<br>before and after the Glass Fire in Central California<br>among 51 County Subdivision" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A3-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A3-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A3-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Random Effects)<br><i>Model A3-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Random Effects)<br><i>Model A3-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Random Effects)<br><i>Model A3-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status and health care quality indices, which were extremely correlated with socioeconomic status and health care capacity; the latter two variables were also split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 8.0",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE,
    custom.gof.names = c(
    rep(NA, 3),
    "s_idios" = "Subdivision Variance", 
    "s_time" = "Temporal Variance")
)


remove(m1,m2,m3,m4,m5,m6,       m7,m8,m9,m10,m11,m12)
```

### Matched by Shelter-in-Place


```{r}
library(lme4)
library(lmerTest)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate")), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_shelter_match.rds")) %>%
  filter(weights > 0)

library(plm)
m1 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m3 <- la %>%
  plm(formula = log(positivity_rate) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(positivity_rate) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(positivity_rate) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_shelter_match.rds")) %>%
  filter(weights > 0)


m7 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m9 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(case_rate) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


# 4/6 of the Lousiana models prefer fixed effects; best to go with fixed effects
phtest(m1,m2)
phtest(m3,m4)
phtest(m5,m6)
# 6/6 of the California models prefer random effects
phtest(m7,m8)
phtest(m9,m10)
phtest(m11,m12)


texreg::htmlreg(
  list(m1,#m2,
       m3,# m4,
       m5,#m6,
       m8,#m7,
       m10,#m9,
       m12#m11
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_shelter.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Shelter-in-Place Behavior",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1,064)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 56 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 1007)<br>before and after the Glass Fire in Central California<br>among 53 County Subdivision" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A4-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A4-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A4-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Random Effects)<br><i>Model A4-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Random Effects)<br><i>Model A4-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Random Effects)<br><i>Model A4-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status and health care quality indices, which were extremely correlated with socioeconomic status and health care capacity; the latter two variables were also split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE,
    custom.gof.names = c(
    rep(NA, 3),
    "s_idios" = "Subdivision Variance", 
    "s_time" = "Temporal Variance")
)


remove(m1,m2,m3,m4,m5,m6,       m7,m8,m9,m10,m11,m12)
```


### DiD Disaster Damage

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(positivity_rate)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_disaster_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame() %>%
  mutate(week = factor(week))


m1 <- la %>%
  zelig(formula = log(positivity_rate) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(positivity_rate) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, 
      model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(positivity_rate) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(case_rate)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_disaster_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(case_rate) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(case_rate) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(case_rate) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_disaster.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Disaster Damage",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 608)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 32 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 437)<br>before and after the Glass Fire in Central California<br>among 23 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A5-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A5-2</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A5-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A5-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A5-5</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A5-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:11,
    "<b>Disaster Damage</b>" = 12:15,
    "<b>Time Variant Controls</b>" = 16:17,
    "<b>Social Capital Indices</b>" = 18:20,
    "<b>Social Vulnerability Indices</b>" = 21:24,
    "<b>Health Care & Quality</b>" = 25:26,
    "<b>Governance</b>" = 27:28),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Tracts",
  "evacuation_intra_more:counter" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Tracts",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Tracts",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Tracts",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```

### DiD Evacuation

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(positivity_rate)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_evac_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()


m1 <- la %>%
  zelig(formula = log(positivity_rate) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(positivity_rate) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(positivity_rate) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(case_rate)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_evac_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(case_rate) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(case_rate) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(case_rate) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_evac.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Evacuation",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1026)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 54 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 969)<br>before and after the Glass Fire in Central California<br>among 51 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A6-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A6-2</i>",
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A6-3</i>)",
    
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A6-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A6-5</i>",
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A6-6</i>)"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All Models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:12,
    "<b>Disaster Damage</b>" = 13:16,
    "<b>Time Variant Controls</b>" = 17:18,
    "<b>Social Capital Indices</b>" = 19:21,
    "<b>Social Vulnerability Indices</b>" = 22:25,
    "<b>Health Care & Quality</b>" = 26:27,
    "<b>Governance</b>" = 28:30),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Tracts",
  "counter:evacuation_intra_more" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Tracts",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Tracts",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Tracts",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```

### DiD Shelter-in-Place

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(positivity_rate)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_shelter_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()


m1 <- la %>%
  zelig(formula = log(positivity_rate) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(positivity_rate) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(positivity_rate) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(case_rate)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_shelter_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(case_rate) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(case_rate) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(case_rate) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_shelter.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Shelter-in-Place Behavior",
  caption.above = TRUE,
  custom.header = list(
    "Test Positivity Rates (n = 1064)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 56 County Subdivisions" = 1:3, 
    "Case Rates per 100,000 residents (n = 1007)<br>before and after the Glass Fire in Central California<br>among 53 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A7-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A7-2</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A7-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A7-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A7-5</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A7-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:11,
    "<b>Disaster Damage</b>" = 12:15,
    "<b>Time Variant Controls</b>" = 16:17,
    "<b>Social Capital Indices</b>" = 18:20,
    "<b>Social Vulnerability Indices</b>" = 21:24,
    "<b>Health Care & Quality</b>" = 25:26,
    "<b>Governance</b>" = 27:28),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Cities",
  "evacuation_intra_more:counter" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Cities",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Cities",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Cities",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```



# 6. Sensitivity Analysis

### 6.1: 1 Week Later

#### Random Effects

```{r}
# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
  # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int, weathertrans),
    funs(scale(.)))

# We almost definitely want to control for time
# It's not really appropriate to control for each municipality with a separate intercept
# especially because after all our covariates, we don't leave much variance there.

# Note: although LA's positivity rates are surprisingly normally distributed for a rate,
# their residuals are not; log-transforming the positivity rates greatly improves the normality of residuals, reducing heteroskedasticity.

#ggplot(mapping = aes(x = m1a$residuals)) + 
#  geom_histogram()

#ggplot(mapping = aes(x = m1b$residuals)) + 
#  geom_histogram()

# Likewise, a likelihood ratio test shows a statistically significant improvement in the log-likelihood when we switch to log-transformation

#lmtest::lrtest(m1b, m1a)
# remove(m1b,m1a)

library(plm)
#la %>%
#  lm(formula = log(rate1) ~ 
#        positivity_rate_lag2 + weathertrans +
#         evacuation_more + 
#         avg_rainfall +
#         bonding + bridging + linking + 
#         svi_socioeconomic + svi_household_disability + 
#         svi_minority + svi_housing_tranport +
#         workplaces_int + health_care_capacity_int + health_quality_int + 
#         employee_muni_int + democrat_2016_int + factor(week)) %>%
#  car::vif() %>%
#  .^2
# weathertrans is super colinearity with week


m1 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"))


m2 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"))

m3 <- la %>%
  plm(formula = log(rate1) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"))


m4 <- la %>%
  plm(formula = log(rate1) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"))

m5 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))

m6 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
  # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int, weathertrans),
      funs(scale(.)))

ca %>%
  lm(formula = log(rate1) ~ 
        case_rate_lag2 + weathertrans +
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int + factor(week)) %>%
  car::vif() %>%
  .^2

m7 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))


m8 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m9 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))

phtest(m7,m8)

texreg::htmlreg(
  list(m1,#m2, 
       m3,#m4,
       m5,#m6,
       m7,#m8,
       m9,#m10,
       m11#,m12
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_1wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i>",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 3,344)<br>before and after Hurricane Zeta<br>in Southeastern Louisiana<br>among 176 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents<br>(n = 3,439) before and after the Glass Fire<br>in Central California<br>among 181 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A1-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A1-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A1-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A1-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A1-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A1-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity in California models, we split up socioeconomic status, minority status, health care capacity, and health care quality indices into four quantiles each. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 7.7.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE, single.row = TRUE,
#  custom.gof.names = c(
#    rep(NA, 3),
#    "s_idios" = "Subdivision Variance", 
#    "s_time" = "Temporal Variance")
)

remove(m1,m2,m3,m4,m5,m6,
       m7,m8,m9,m10,m11,m12)
```



#### Matched by Disaster

```{r}


# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%

  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int, weathertrans),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_disaster_match.rds")) %>%
  filter(weights > 0)

# Check that colinearity is below 10 for all
#la %>%
#  lm(formula = log(rate1) ~ 
#        positivity_rate_lag2 + weathertrans +
#         evacuation_more + 
#         avg_rainfall +
#         bonding + bridging + linking + 
#         svi_socioeconomic + svi_household_disability + 
#         svi_minority + svi_housing_tranport +
#        workplaces_int + health_care_capacity_int + health_quality_int + 
#         employee_muni_int + democrat_2016_int +
#        factor(week), weights = weights) %>%
#  car::vif() %>%
#  .^2
# Produces enormous colinearity between weathertrans (135) avg_rainfall (9.4) and week
# Can't control for weathertrans

library(plm)
m1 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)


m3 <- la %>%
  plm(formula = log(rate1) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(rate1) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
  # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = svi_minority,
         bonding = bonding) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int, weathertrans),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_disaster_match.rds")) %>%
  filter(weights > 0)



# In our matched models in California, socioeconomic status was strongly correlated with minority status, health care capacity, and health care quality. To reduce this colinearity, we had to remove minority status, health care capacity, and health care quality from models. Since they are highly correlated, their omission is not problematic, as they would be explaining the same variation anyway. 


ca %>%
  lm(formula = log(rate1) ~ 
        case_rate_lag2 + #weathertrans +
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int + factor(week),
     weights = weights) %>%
  car::vif() %>%
  .^2
# Produces really high colinearity (14.8) with weathertrans

m7 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

phtest(m7,m8)

m9 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


texreg::htmlreg(
  list(m1,#m2,
       m3,#m4,
       m5,#m6,
       m7,#m8,
       m9,#m10,
       m11#m12
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_disaster_1wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Disaster Damage",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 608)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 32 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 437)<br>before and after the Glass Fire in Central California<br>among 23 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A2-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A2-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A2-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A2-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A2-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A2-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status, health care capacity, and health care quality indices, which were extremely correlated with socioeconomic status and bonding social capital. Socioeconomic status also was split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 7.5.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE
#   custom.gof.names = c(
#    rep(NA, 3),
#    "s_idios" = "Subdivision Variance", 
#    "s_time" = "Temporal Variance")
)

#remove(m1,m2,m3,m4,m5,m6,
#       m7,m8,m9,m10,m11,m12)
```


#### Matched by Evacuation

```{r}
library(lme4)
library(lmerTest)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%

  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_evac_match.rds")) %>%
  filter(weights > 0)

library(plm)
m1 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m3 <- la %>%
  plm(formula = log(rate1) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(rate1) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
  # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_evac_match.rds")) %>%
  filter(weights > 0)


m7 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m9 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


texreg::htmlreg(
  list(m1,#m2,
       m3,# m4,
       m5,#m6,
       m8,#m7,
       m10,#m9,
       m12#m11
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_evac_1wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Evacuation",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1,026)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 54 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 969)<br>before and after the Glass Fire in Central California<br>among 51 County Subdivision" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A3-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A3-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A3-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Random Effects)<br><i>Model A3-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Random Effects)<br><i>Model A3-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Random Effects)<br><i>Model A3-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status and health care quality indices, which were extremely correlated with socioeconomic status and health care capacity; the latter two variables were also split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 8.0",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE,
    custom.gof.names = c(
    rep(NA, 3),
    "s_idios" = "Subdivision Variance", 
    "s_time" = "Temporal Variance")
)


remove(m1,m2,m3,m4,m5,m6,       m7,m8,m9,m10,m11,m12)
```

#### Matched by Shelter-in-Place


```{r}
library(lme4)
library(lmerTest)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%

  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_shelter_match.rds")) %>%
  filter(weights > 0)

library(plm)
m1 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m3 <- la %>%
  plm(formula = log(rate1) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(rate1) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(rate1) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
  # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_shelter_match.rds")) %>%
  filter(weights > 0)


m7 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m9 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate1) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


# 4/6 of the Lousiana models prefer fixed effects; best to go with fixed effects
phtest(m1,m2)
phtest(m3,m4)
phtest(m5,m6)
# 6/6 of the California models prefer random effects
phtest(m7,m8)
phtest(m9,m10)
phtest(m11,m12)


texreg::htmlreg(
  list(m1,#m2,
       m3,# m4,
       m5,#m6,
       m8,#m7,
       m10,#m9,
       m12#m11
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_shelter_1wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Shelter-in-Place Behavior",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1,064)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 56 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 1007)<br>before and after the Glass Fire in Central California<br>among 53 County Subdivision" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A4-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A4-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A4-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Random Effects)<br><i>Model A4-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Random Effects)<br><i>Model A4-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Random Effects)<br><i>Model A4-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status and health care quality indices, which were extremely correlated with socioeconomic status and health care capacity; the latter two variables were also split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE,
    custom.gof.names = c(
    rep(NA, 3),
    "s_idios" = "Subdivision Variance", 
    "s_time" = "Temporal Variance")
)


remove(m1,m2,m3,m4,m5,m6,       m7,m8,m9,m10,m11,m12)
```

#### DiD Disaster Damage

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_disaster_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame() %>%
  mutate(week = factor(week))


m1 <- la %>%
  zelig(formula = log(rate1) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(rate1) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, 
      model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(rate1) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_disaster_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(rate1) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(rate1) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(rate1) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_disaster_1wklater.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Disaster Damage",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 608)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 32 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 437)<br>before and after the Glass Fire in Central California<br>among 23 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A5-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A5-2</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A5-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A5-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A5-5</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A5-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:11,
    "<b>Disaster Damage</b>" = 12:15,
    "<b>Time Variant Controls</b>" = 16:17,
    "<b>Social Capital Indices</b>" = 18:20,
    "<b>Social Vulnerability Indices</b>" = 21:24,
    "<b>Health Care & Quality</b>" = 25:26,
    "<b>Governance</b>" = 27:28),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Tracts",
  "evacuation_intra_more:counter" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Tracts",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Tracts",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Tracts",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```

#### DiD Evacuation

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_evac_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()


m1 <- la %>%
  zelig(formula = log(rate1) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(rate1) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(rate1) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_evac_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(rate1) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(rate1) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(rate1) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_evac_1wklater.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Evacuation",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1026)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 54 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 969)<br>before and after the Glass Fire in Central California<br>among 51 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A6-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A6-2</i>",
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A6-3</i>)",
    
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A6-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A6-5</i>",
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A6-6</i>)"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All Models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:12,
    "<b>Disaster Damage</b>" = 13:16,
    "<b>Time Variant Controls</b>" = 17:18,
    "<b>Social Capital Indices</b>" = 19:21,
    "<b>Social Vulnerability Indices</b>" = 22:25,
    "<b>Health Care & Quality</b>" = 26:27,
    "<b>Governance</b>" = 28:30),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Tracts",
  "counter:evacuation_intra_more" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Tracts",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Tracts",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Tracts",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```

#### DiD Shelter-in-Place

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_shelter_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()


m1 <- la %>%
  zelig(formula = log(rate1) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(rate1) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(rate1) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate1)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate1), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_shelter_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(rate1) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(rate1) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(rate1) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_shelter_1wklater.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Shelter-in-Place Behavior",
  caption.above = TRUE,
  custom.header = list(
    "Test Positivity Rates (n = 1064)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 56 County Subdivisions" = 1:3, 
    "Case Rates per 100,000 residents (n = 1007)<br>before and after the Glass Fire in Central California<br>among 53 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A7-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A7-2</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A7-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A7-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A7-5</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A7-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:11,
    "<b>Disaster Damage</b>" = 12:15,
    "<b>Time Variant Controls</b>" = 16:17,
    "<b>Social Capital Indices</b>" = 18:20,
    "<b>Social Vulnerability Indices</b>" = 21:24,
    "<b>Health Care & Quality</b>" = 25:26,
    "<b>Governance</b>" = 27:28),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Cities",
  "evacuation_intra_more:counter" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Cities",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Cities",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Cities",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```

### 6.2: 3 Weeks Later

#### Random Effects

```{r}
# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
  # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int, weathertrans),
    funs(scale(.)))

# We almost definitely want to control for time
# It's not really appropriate to control for each municipality with a separate intercept
# especially because after all our covariates, we don't leave much variance there.

# Note: although LA's positivity rates are surprisingly normally distributed for a rate,
# their residuals are not; log-transforming the positivity rates greatly improves the normality of residuals, reducing heteroskedasticity.

#ggplot(mapping = aes(x = m1a$residuals)) + 
#  geom_histogram()

#ggplot(mapping = aes(x = m1b$residuals)) + 
#  geom_histogram()

# Likewise, a likelihood ratio test shows a statistically significant improvement in the log-likelihood when we switch to log-transformation

#lmtest::lrtest(m1b, m1a)
# remove(m1b,m1a)

library(plm)
#la %>%
#  lm(formula = log(rate3) ~ 
#        positivity_rate_lag2 + weathertrans +
#         evacuation_more + 
#         avg_rainfall +
#         bonding + bridging + linking + 
#         svi_socioeconomic + svi_household_disability + 
#         svi_minority + svi_housing_tranport +
#         workplaces_int + health_care_capacity_int + health_quality_int + 
#         employee_muni_int + democrat_2016_int + factor(week)) %>%
#  car::vif() %>%
#  .^2
# weathertrans is super colinearity with week


m1 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"))


m2 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"))

m3 <- la %>%
  plm(formula = log(rate3) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"))


m4 <- la %>%
  plm(formula = log(rate3) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"))

m5 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))

m6 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int, weathertrans),
      funs(scale(.)))

ca %>%
  lm(formula = log(rate3) ~ 
        case_rate_lag2 + weathertrans +
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int + factor(week)) %>%
  car::vif() %>%
  .^2

m7 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))


m8 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m9 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))

phtest(m7,m8)

texreg::htmlreg(
  list(m1,#m2, 
       m3,#m4,
       m5,#m6,
       m7,#m8,
       m9,#m10,
       m11#,m12
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_3wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i>",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 3,344)<br>before and after Hurricane Zeta<br>in Southeastern Louisiana<br>among 176 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents<br>(n = 3,439) before and after the Glass Fire<br>in Central California<br>among 181 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A1-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A1-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A1-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A1-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A1-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A1-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity in California models, we split up socioeconomic status, minority status, health care capacity, and health care quality indices into four quantiles each. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 7.7.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE, single.row = TRUE,
#  custom.gof.names = c(
#    rep(NA, 3),
#    "s_idios" = "Subdivision Variance", 
#    "s_time" = "Temporal Variance")
)

remove(m1,m2,m3,m4,m5,m6,
       m7,m8,m9,m10,m11,m12)
```



#### Matched by Disaster

```{r}


# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%

  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int, weathertrans),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_disaster_match.rds")) %>%
  filter(weights > 0)

# Check that colinearity is below 10 for all
#la %>%
#  lm(formula = log(rate3) ~ 
#        positivity_rate_lag2 + weathertrans +
#         evacuation_more + 
#         avg_rainfall +
#         bonding + bridging + linking + 
#         svi_socioeconomic + svi_household_disability + 
#         svi_minority + svi_housing_tranport +
#        workplaces_int + health_care_capacity_int + health_quality_int + 
#         employee_muni_int + democrat_2016_int +
#        factor(week), weights = weights) %>%
#  car::vif() %>%
#  .^2
# Produces enormous colinearity between weathertrans (135) avg_rainfall (9.4) and week
# Can't control for weathertrans

library(plm)
m1 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)


m3 <- la %>%
  plm(formula = log(rate3) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(rate3) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
     # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%
 
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = svi_minority,
         bonding = bonding) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int, weathertrans),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_disaster_match.rds")) %>%
  filter(weights > 0)



# In our matched models in California, socioeconomic status was strongly correlated with minority status, health care capacity, and health care quality. To reduce this colinearity, we had to remove minority status, health care capacity, and health care quality from models. Since they are highly correlated, their omission is not problematic, as they would be explaining the same variation anyway. 


ca %>%
  lm(formula = log(rate3) ~ 
        case_rate_lag2 + #weathertrans +
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int + factor(week),
     weights = weights) %>%
  car::vif() %>%
  .^2
# Produces really high colinearity (14.8) with weathertrans

m7 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

phtest(m7,m8)

m9 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


texreg::htmlreg(
  list(m1,#m2,
       m3,#m4,
       m5,#m6,
       m7,#m8,
       m9,#m10,
       m11#m12
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_disaster_3wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Disaster Damage",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 608)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 32 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 437)<br>before and after the Glass Fire in Central California<br>among 23 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A2-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A2-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A2-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A2-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A2-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A2-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status, health care capacity, and health care quality indices, which were extremely correlated with socioeconomic status and bonding social capital. Socioeconomic status also was split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 7.5.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE
#   custom.gof.names = c(
#    rep(NA, 3),
#    "s_idios" = "Subdivision Variance", 
#    "s_time" = "Temporal Variance")
)

#remove(m1,m2,m3,m4,m5,m6,
#       m7,m8,m9,m10,m11,m12)
```


#### Matched by Evacuation

```{r}
library(lme4)
library(lmerTest)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%

  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_evac_match.rds")) %>%
  filter(weights > 0)

library(plm)
m1 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m3 <- la %>%
  plm(formula = log(rate3) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(rate3) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
     # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%
 
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2,rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_evac_match.rds")) %>%
  filter(weights > 0)


m7 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m9 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


texreg::htmlreg(
  list(m1,#m2,
       m3,# m4,
       m5,#m6,
       m8,#m7,
       m10,#m9,
       m12#m11
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_evac_3wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Evacuation",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1,026)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 54 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 969)<br>before and after the Glass Fire in Central California<br>among 51 County Subdivision" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A3-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A3-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A3-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Random Effects)<br><i>Model A3-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Random Effects)<br><i>Model A3-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Random Effects)<br><i>Model A3-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status and health care quality indices, which were extremely correlated with socioeconomic status and health care capacity; the latter two variables were also split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 8.0",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE,
    custom.gof.names = c(
    rep(NA, 3),
    "s_idios" = "Subdivision Variance", 
    "s_time" = "Temporal Variance")
)


remove(m1,m2,m3,m4,m5,m6,       m7,m8,m9,m10,m11,m12)
```

#### Matched by Shelter-in-Place


```{r}
library(lme4)
library(lmerTest)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%

  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_shelter_match.rds")) %>%
  filter(weights > 0)

library(plm)
m1 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m3 <- la %>%
  plm(formula = log(rate3) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(rate3) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(rate3) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
     # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%
 
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2,rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_shelter_match.rds")) %>%
  filter(weights > 0)


m7 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m9 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate3) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


# 4/6 of the Lousiana models prefer fixed effects; best to go with fixed effects
phtest(m1,m2)
phtest(m3,m4)
phtest(m5,m6)
# 6/6 of the California models prefer random effects
phtest(m7,m8)
phtest(m9,m10)
phtest(m11,m12)


texreg::htmlreg(
  list(m1,#m2,
       m3,# m4,
       m5,#m6,
       m8,#m7,
       m10,#m9,
       m12#m11
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_shelter_3wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Shelter-in-Place Behavior",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1,064)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 56 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 1007)<br>before and after the Glass Fire in Central California<br>among 53 County Subdivision" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A4-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A4-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A4-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Random Effects)<br><i>Model A4-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Random Effects)<br><i>Model A4-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Random Effects)<br><i>Model A4-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status and health care quality indices, which were extremely correlated with socioeconomic status and health care capacity; the latter two variables were also split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE,
    custom.gof.names = c(
    rep(NA, 3),
    "s_idios" = "Subdivision Variance", 
    "s_time" = "Temporal Variance")
)


remove(m1,m2,m3,m4,m5,m6,       m7,m8,m9,m10,m11,m12)
```

#### DiD Disaster Damage

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_disaster_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame() %>%
  mutate(week = factor(week))


m1 <- la %>%
  zelig(formula = log(rate3) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(rate3) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, 
      model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(rate3) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_disaster_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(rate3) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(rate3) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(rate3) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_disaster_3wklater.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Disaster Damage",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 608)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 32 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 437)<br>before and after the Glass Fire in Central California<br>among 23 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A5-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A5-2</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A5-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A5-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A5-5</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A5-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:11,
    "<b>Disaster Damage</b>" = 12:15,
    "<b>Time Variant Controls</b>" = 16:17,
    "<b>Social Capital Indices</b>" = 18:20,
    "<b>Social Vulnerability Indices</b>" = 21:24,
    "<b>Health Care & Quality</b>" = 25:26,
    "<b>Governance</b>" = 27:28),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Tracts",
  "evacuation_intra_more:counter" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Tracts",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Tracts",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Tracts",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```

#### DiD Evacuation

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_evac_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()


m1 <- la %>%
  zelig(formula = log(rate3) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(rate3) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(rate3) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_evac_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(rate3) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(rate3) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(rate3) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_evac_3wklater.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Evacuation",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1026)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 54 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 969)<br>before and after the Glass Fire in Central California<br>among 51 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A6-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A6-2</i>",
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A6-3</i>)",
    
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A6-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A6-5</i>",
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A6-6</i>)"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All Models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:12,
    "<b>Disaster Damage</b>" = 13:16,
    "<b>Time Variant Controls</b>" = 17:18,
    "<b>Social Capital Indices</b>" = 19:21,
    "<b>Social Vulnerability Indices</b>" = 22:25,
    "<b>Health Care & Quality</b>" = 26:27,
    "<b>Governance</b>" = 28:30),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Tracts",
  "counter:evacuation_intra_more" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Tracts",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Tracts",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Tracts",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```

#### DiD Shelter-in-Place

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_shelter_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()


m1 <- la %>%
  zelig(formula = log(rate3) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(rate3) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(rate3) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate3)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate3), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_shelter_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(rate3) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(rate3) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(rate3) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_shelter_3wklater.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Shelter-in-Place Behavior",
  caption.above = TRUE,
  custom.header = list(
    "Test Positivity Rates (n = 1064)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 56 County Subdivisions" = 1:3, 
    "Case Rates per 100,000 residents (n = 1007)<br>before and after the Glass Fire in Central California<br>among 53 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A7-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A7-2</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A7-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A7-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A7-5</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A7-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:11,
    "<b>Disaster Damage</b>" = 12:15,
    "<b>Time Variant Controls</b>" = 16:17,
    "<b>Social Capital Indices</b>" = 18:20,
    "<b>Social Vulnerability Indices</b>" = 21:24,
    "<b>Health Care & Quality</b>" = 25:26,
    "<b>Governance</b>" = 27:28),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Cities",
  "evacuation_intra_more:counter" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Cities",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Cities",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Cities",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```


### 6.3: 4 Weeks Later

#### Random Effects

```{r}
# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
  # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int, weathertrans),
    funs(scale(.)))

# We almost definitely want to control for time
# It's not really appropriate to control for each municipality with a separate intercept
# especially because after all our covariates, we don't leave much variance there.

# Note: although LA's positivity rates are surprisingly normally distributed for a rate,
# their residuals are not; log-transforming the positivity rates greatly improves the normality of residuals, reducing heteroskedasticity.

#ggplot(mapping = aes(x = m1a$residuals)) + 
#  geom_histogram()

#ggplot(mapping = aes(x = m1b$residuals)) + 
#  geom_histogram()

# Likewise, a likelihood ratio test shows a statistically significant improvement in the log-likelihood when we switch to log-transformation

#lmtest::lrtest(m1b, m1a)
# remove(m1b,m1a)

library(plm)
#la %>%
#  lm(formula = log(rate4) ~ 
#        positivity_rate_lag2 + weathertrans +
#         evacuation_more + 
#         avg_rainfall +
#         bonding + bridging + linking + 
#         svi_socioeconomic + svi_household_disability + 
#         svi_minority + svi_housing_tranport +
#         workplaces_int + health_care_capacity_int + health_quality_int + 
#         employee_muni_int + democrat_2016_int + factor(week)) %>%
#  car::vif() %>%
#  .^2
# weathertrans is super colinearity with week


m1 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"))


m2 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"))

m3 <- la %>%
  plm(formula = log(rate4) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"))


m4 <- la %>%
  plm(formula = log(rate4) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"))

m5 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))

m6 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int, weathertrans),
      funs(scale(.)))

ca %>%
  lm(formula = log(rate4) ~ 
        case_rate_lag2 + weathertrans +
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int + factor(week)) %>%
  car::vif() %>%
  .^2

m7 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))


m8 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m9 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))

phtest(m7,m8)

texreg::htmlreg(
  list(m1,#m2, 
       m3,#m4,
       m5,#m6,
       m7,#m8,
       m9,#m10,
       m11#,m12
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_4wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i>",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 3,344)<br>before and after Hurricane Zeta<br>in Southeastern Louisiana<br>among 176 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents<br>(n = 3,439) before and after the Glass Fire<br>in Central California<br>among 181 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A1-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A1-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A1-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A1-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A1-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A1-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity in California models, we split up socioeconomic status, minority status, health care capacity, and health care quality indices into four quantiles each. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 7.7.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE, single.row = TRUE,
#  custom.gof.names = c(
#    rep(NA, 3),
#    "s_idios" = "Subdivision Variance", 
#    "s_time" = "Temporal Variance")
)

remove(m1,m2,m3,m4,m5,m6,
       m7,m8,m9,m10,m11,m12)
```



#### Matched by Disaster

```{r}


# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%

  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int, weathertrans),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_disaster_match.rds")) %>%
  filter(weights > 0)

# Check that colinearity is below 10 for all
#la %>%
#  lm(formula = log(rate4) ~ 
#        positivity_rate_lag2 + weathertrans +
#         evacuation_more + 
#         avg_rainfall +
#         bonding + bridging + linking + 
#         svi_socioeconomic + svi_household_disability + 
#         svi_minority + svi_housing_tranport +
#        workplaces_int + health_care_capacity_int + health_quality_int + 
#         employee_muni_int + democrat_2016_int +
#        factor(week), weights = weights) %>%
#  car::vif() %>%
#  .^2
# Produces enormous colinearity between weathertrans (135) avg_rainfall (9.4) and week
# Can't control for weathertrans

library(plm)
m1 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)


m3 <- la %>%
  plm(formula = log(rate4) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(rate4) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
     # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%
 
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = svi_minority,
         bonding = bonding) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int, weathertrans),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_disaster_match.rds")) %>%
  filter(weights > 0)



# In our matched models in California, socioeconomic status was strongly correlated with minority status, health care capacity, and health care quality. To reduce this colinearity, we had to remove minority status, health care capacity, and health care quality from models. Since they are highly correlated, their omission is not problematic, as they would be explaining the same variation anyway. 


ca %>%
  lm(formula = log(rate4) ~ 
        case_rate_lag2 + #weathertrans +
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int + factor(week),
     weights = weights) %>%
  car::vif() %>%
  .^2
# Produces really high colinearity (14.8) with weathertrans

m7 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

phtest(m7,m8)

m9 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


texreg::htmlreg(
  list(m1,#m2,
       m3,#m4,
       m5,#m6,
       m7,#m8,
       m9,#m10,
       m11#m12
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_disaster_4wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Disaster Damage",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 608)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 32 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 437)<br>before and after the Glass Fire in Central California<br>among 23 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A2-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A2-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A2-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A2-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A2-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A2-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status, health care capacity, and health care quality indices, which were extremely correlated with socioeconomic status and bonding social capital. Socioeconomic status also was split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 7.5.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE
#   custom.gof.names = c(
#    rep(NA, 3),
#    "s_idios" = "Subdivision Variance", 
#    "s_time" = "Temporal Variance")
)

#remove(m1,m2,m3,m4,m5,m6,
#       m7,m8,m9,m10,m11,m12)
```


#### Matched by Evacuation

```{r}
library(lme4)
library(lmerTest)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%

  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_evac_match.rds")) %>%
  filter(weights > 0)

library(plm)
m1 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m3 <- la %>%
  plm(formula = log(rate4) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(rate4) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
     # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%
 
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2,rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_evac_match.rds")) %>%
  filter(weights > 0)


m7 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m9 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


texreg::htmlreg(
  list(m1,#m2,
       m3,# m4,
       m5,#m6,
       m8,#m7,
       m10,#m9,
       m12#m11
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_evac_4wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Evacuation",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1,026)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 54 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 969)<br>before and after the Glass Fire in Central California<br>among 51 County Subdivision" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A3-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A3-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A3-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Random Effects)<br><i>Model A3-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Random Effects)<br><i>Model A3-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Random Effects)<br><i>Model A3-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status and health care quality indices, which were extremely correlated with socioeconomic status and health care capacity; the latter two variables were also split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 8.0",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE,
    custom.gof.names = c(
    rep(NA, 3),
    "s_idios" = "Subdivision Variance", 
    "s_time" = "Temporal Variance")
)


remove(m1,m2,m3,m4,m5,m6,       m7,m8,m9,m10,m11,m12)
```

#### Matched by Shelter-in-Place


```{r}
library(lme4)
library(lmerTest)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%

  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_shelter_match.rds")) %>%
  filter(weights > 0)

library(plm)
m1 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m3 <- la %>%
  plm(formula = log(rate4) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(rate4) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(rate4) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
     # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%
 
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2,rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_shelter_match.rds")) %>%
  filter(weights > 0)


m7 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m9 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate4) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


# 4/6 of the Lousiana models prefer fixed effects; best to go with fixed effects
phtest(m1,m2)
phtest(m3,m4)
phtest(m5,m6)
# 6/6 of the California models prefer random effects
phtest(m7,m8)
phtest(m9,m10)
phtest(m11,m12)


texreg::htmlreg(
  list(m1,#m2,
       m3,# m4,
       m5,#m6,
       m8,#m7,
       m10,#m9,
       m12#m11
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_shelter_4wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Shelter-in-Place Behavior",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1,064)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 56 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 1007)<br>before and after the Glass Fire in Central California<br>among 53 County Subdivision" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A4-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A4-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A4-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Random Effects)<br><i>Model A4-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Random Effects)<br><i>Model A4-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Random Effects)<br><i>Model A4-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status and health care quality indices, which were extremely correlated with socioeconomic status and health care capacity; the latter two variables were also split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE,
    custom.gof.names = c(
    rep(NA, 3),
    "s_idios" = "Subdivision Variance", 
    "s_time" = "Temporal Variance")
)


remove(m1,m2,m3,m4,m5,m6,       m7,m8,m9,m10,m11,m12)
```


#### DiD Disaster Damage

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_disaster_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame() %>%
  mutate(week = factor(week))


m1 <- la %>%
  zelig(formula = log(rate4) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(rate4) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, 
      model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(rate4) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_disaster_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(rate4) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(rate4) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(rate4) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_disaster_4wklater.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Disaster Damage",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 608)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 32 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 437)<br>before and after the Glass Fire in Central California<br>among 23 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A5-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A5-2</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A5-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A5-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A5-5</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A5-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:11,
    "<b>Disaster Damage</b>" = 12:15,
    "<b>Time Variant Controls</b>" = 16:17,
    "<b>Social Capital Indices</b>" = 18:20,
    "<b>Social Vulnerability Indices</b>" = 21:24,
    "<b>Health Care & Quality</b>" = 25:26,
    "<b>Governance</b>" = 27:28),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Tracts",
  "evacuation_intra_more:counter" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Tracts",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Tracts",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Tracts",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```

#### DiD Evacuation

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_evac_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()


m1 <- la %>%
  zelig(formula = log(rate4) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(rate4) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(rate4) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_evac_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(rate4) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(rate4) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(rate4) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_evac_4wklater.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Evacuation",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1026)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 54 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 969)<br>before and after the Glass Fire in Central California<br>among 51 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A6-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A6-2</i>",
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A6-3</i>)",
    
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A6-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A6-5</i>",
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A6-6</i>)"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All Models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:12,
    "<b>Disaster Damage</b>" = 13:16,
    "<b>Time Variant Controls</b>" = 17:18,
    "<b>Social Capital Indices</b>" = 19:21,
    "<b>Social Vulnerability Indices</b>" = 22:25,
    "<b>Health Care & Quality</b>" = 26:27,
    "<b>Governance</b>" = 28:30),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Tracts",
  "counter:evacuation_intra_more" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Tracts",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Tracts",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Tracts",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```

#### DiD Shelter-in-Place

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_shelter_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()


m1 <- la %>%
  zelig(formula = log(rate4) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(rate4) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(rate4) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate4)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate4), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_shelter_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(rate4) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(rate4) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(rate4) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_shelter_4wklater.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Shelter-in-Place Behavior",
  caption.above = TRUE,
  custom.header = list(
    "Test Positivity Rates (n = 1064)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 56 County Subdivisions" = 1:3, 
    "Case Rates per 100,000 residents (n = 1007)<br>before and after the Glass Fire in Central California<br>among 53 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A7-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A7-2</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A7-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A7-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A7-5</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A7-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:11,
    "<b>Disaster Damage</b>" = 12:15,
    "<b>Time Variant Controls</b>" = 16:17,
    "<b>Social Capital Indices</b>" = 18:20,
    "<b>Social Vulnerability Indices</b>" = 21:24,
    "<b>Health Care & Quality</b>" = 25:26,
    "<b>Governance</b>" = 27:28),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Cities",
  "evacuation_intra_more:counter" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Cities",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Cities",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Cities",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```



### 6.4: 5 Weeks Later

#### Random Effects

```{r}
# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
  # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int, weathertrans),
    funs(scale(.)))

# We almost definitely want to control for time
# It's not really appropriate to control for each municipality with a separate intercept
# especially because after all our covariates, we don't leave much variance there.

# Note: although LA's positivity rates are surprisingly normally distributed for a rate,
# their residuals are not; log-transforming the positivity rates greatly improves the normality of residuals, reducing heteroskedasticity.

#ggplot(mapping = aes(x = m1a$residuals)) + 
#  geom_histogram()

#ggplot(mapping = aes(x = m1b$residuals)) + 
#  geom_histogram()

# Likewise, a likelihood ratio test shows a statistically significant improvement in the log-likelihood when we switch to log-transformation

#lmtest::lrtest(m1b, m1a)
# remove(m1b,m1a)

library(plm)
#la %>%
#  lm(formula = log(rate5) ~ 
#        positivity_rate_lag2 + weathertrans +
#         evacuation_more + 
#         avg_rainfall +
#         bonding + bridging + linking + 
#         svi_socioeconomic + svi_household_disability + 
#         svi_minority + svi_housing_tranport +
#         workplaces_int + health_care_capacity_int + health_quality_int + 
#         employee_muni_int + democrat_2016_int + factor(week)) %>%
#  car::vif() %>%
#  .^2
# weathertrans is super colinearity with week


m1 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"))


m2 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"))

m3 <- la %>%
  plm(formula = log(rate5) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"))


m4 <- la %>%
  plm(formula = log(rate5) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"))

m5 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))

m6 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int, weathertrans),
      funs(scale(.)))

ca %>%
  lm(formula = log(rate5) ~ 
        case_rate_lag2 + weathertrans +
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int + factor(week)) %>%
  car::vif() %>%
  .^2

m7 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))


m8 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m9 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))

phtest(m7,m8)

texreg::htmlreg(
  list(m1,#m2, 
       m3,#m4,
       m5,#m6,
       m7,#m8,
       m9,#m10,
       m11#,m12
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_5wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i>",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 3,344)<br>before and after Hurricane Zeta<br>in Southeastern Louisiana<br>among 176 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents<br>(n = 3,439) before and after the Glass Fire<br>in Central California<br>among 181 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A1-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A1-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A1-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A1-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A1-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A1-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity in California models, we split up socioeconomic status, minority status, health care capacity, and health care quality indices into four quantiles each. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 7.7.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE, single.row = TRUE,
#  custom.gof.names = c(
#    rep(NA, 3),
#    "s_idios" = "Subdivision Variance", 
#    "s_time" = "Temporal Variance")
)

remove(m1,m2,m3,m4,m5,m6,
       m7,m8,m9,m10,m11,m12)
```



#### Matched by Disaster

```{r}


# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%

  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int, weathertrans),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_disaster_match.rds")) %>%
  filter(weights > 0)

# Check that colinearity is below 10 for all
#la %>%
#  lm(formula = log(rate5) ~ 
#        positivity_rate_lag2 + weathertrans +
#         evacuation_more + 
#         avg_rainfall +
#         bonding + bridging + linking + 
#         svi_socioeconomic + svi_household_disability + 
#         svi_minority + svi_housing_tranport +
#        workplaces_int + health_care_capacity_int + health_quality_int + 
#         employee_muni_int + democrat_2016_int +
#        factor(week), weights = weights) %>%
#  car::vif() %>%
#  .^2
# Produces enormous colinearity between weathertrans (135) avg_rainfall (9.4) and week
# Can't control for weathertrans

library(plm)
m1 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)


m3 <- la %>%
  plm(formula = log(rate5) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(rate5) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
     # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%
 
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = svi_minority,
         bonding = bonding) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int, weathertrans),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_disaster_match.rds")) %>%
  filter(weights > 0)



# In our matched models in California, socioeconomic status was strongly correlated with minority status, health care capacity, and health care quality. To reduce this colinearity, we had to remove minority status, health care capacity, and health care quality from models. Since they are highly correlated, their omission is not problematic, as they would be explaining the same variation anyway. 


ca %>%
  lm(formula = log(rate5) ~ 
        case_rate_lag2 + #weathertrans +
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int + factor(week),
     weights = weights) %>%
  car::vif() %>%
  .^2
# Produces really high colinearity (14.8) with weathertrans

m7 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        # svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

phtest(m7,m8)

m9 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + #health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


texreg::htmlreg(
  list(m1,#m2,
       m3,#m4,
       m5,#m6,
       m7,#m8,
       m9,#m10,
       m11#m12
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_disaster_5wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Disaster Damage",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 608)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 32 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 437)<br>before and after the Glass Fire in Central California<br>among 23 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A2-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A2-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A2-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A2-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A2-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A2-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status, health care capacity, and health care quality indices, which were extremely correlated with socioeconomic status and bonding social capital. Socioeconomic status also was split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 7.5.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE
#   custom.gof.names = c(
#    rep(NA, 3),
#    "s_idios" = "Subdivision Variance", 
#    "s_time" = "Temporal Variance")
)

#remove(m1,m2,m3,m4,m5,m6,
#       m7,m8,m9,m10,m11,m12)
```


#### Matched by Evacuation

```{r}
library(lme4)
library(lmerTest)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%

  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_evac_match.rds")) %>%
  filter(weights > 0)

library(plm)
m1 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m3 <- la %>%
  plm(formula = log(rate5) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(rate5) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)



# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
     # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%
 
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2,rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_evac_match.rds")) %>%
  filter(weights > 0)


m7 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m9 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


texreg::htmlreg(
  list(m1,#m2,
       m3,# m4,
       m5,#m6,
       m8,#m7,
       m10,#m9,
       m12#m11
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_evac_5wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Evacuation",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1,026)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 54 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 969)<br>before and after the Glass Fire in Central California<br>among 51 County Subdivision" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A3-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A3-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A3-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Random Effects)<br><i>Model A3-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Random Effects)<br><i>Model A3-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Random Effects)<br><i>Model A3-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status and health care quality indices, which were extremely correlated with socioeconomic status and health care capacity; the latter two variables were also split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2; the highest was 8.0",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE,
    custom.gof.names = c(
    rep(NA, 3),
    "s_idios" = "Subdivision Variance", 
    "s_time" = "Temporal Variance")
)


remove(m1,m2,m3,m4,m5,m6,       m7,m8,m9,m10,m11,m12)
```

#### Matched by Shelter-in-Place


```{r}
library(lme4)
library(lmerTest)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%

  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    positivity_rate_lag2,
    avg_rainfall, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_shelter_match.rds")) %>%
  filter(weights > 0)

library(plm)
m1 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m2 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
         evacuation_more + 
         avg_rainfall +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m3 <- la %>%
  plm(formula = log(rate5) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)


m4 <- la %>%
  plm(formula = log(rate5) ~ 
         positivity_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int, 
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m5 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m6 <- la %>%
  plm(formula = log(rate5) ~ 
        positivity_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       avg_rainfall +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
     # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%
 
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2,rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
  # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_shelter_match.rds")) %>%
  filter(weights > 0)


m7 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
        #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"), weights = weights)

m8 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 + 
        evacuation_more + 
         burn_rate_int +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"), weights = weights)

m9 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority +
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "within", effect = "time", index = c("csub", "week"))
m10 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_more + 
         evacuation_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
      model = "random", effect = "time", index = c("csub", "week"))

m11 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "within", effect = "time", index = c("csub", "week"))
m12 <- ca %>%
  plm(formula = log(rate5) ~ 
        case_rate_lag2 +
        evacuation_intra_more + evacuation_inter_more +
        evacuation_intra_less + evacuation_inter_less +
       burn_rate_int +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int,
 model = "random", effect = "time", index = c("csub", "week"))


# 4/6 of the Lousiana models prefer fixed effects; best to go with fixed effects
phtest(m1,m2)
phtest(m3,m4)
phtest(m5,m6)
# 6/6 of the California models prefer random effects
phtest(m7,m8)
phtest(m9,m10)
phtest(m11,m12)


texreg::htmlreg(
  list(m1,#m2,
       m3,# m4,
       m5,#m6,
       m8,#m7,
       m10,#m9,
       m12#m11
       ),
    bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_1_shelter_5wklater.html",
  caption = "<b>Panel OLS Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 24, 2020</i><br>With Matched Samples and Coarsened Exact Matching Weights to Test Effect of Shelter-in-Place Behavior",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1,064)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 56 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 1007)<br>before and after the Glass Fire in Central California<br>among 53 County Subdivision" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A4-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A4-2</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Fixed Effects)<br><i>Model A4-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Random Effects)<br><i>Model A4-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Random Effects)<br><i>Model A4-5</i>",
    "4 Types of Movement<br>Within & Between Cities<br>(Random Effects)<br><i>Model A4-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All evacuation measures and average rainfall lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. To reduce colinearity, for California models, we removed minority status and health care quality indices, which were extremely correlated with socioeconomic status and health care capacity; the latter two variables were also split into 4 quartiles. All models had variance inflation factors (VIF) below 10, typically near 2.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:6,
    "<b>Time Variant Controls</b>" = 7:10,
    "<b>Social Capital Indices</b>" = 11:13,
    "<b>Social Vulnerability Indices</b>" = 14:17,
    "<b>Health Care & Quality</b>" = 18:19,
    "<b>Governance</b>" = 20:21),
  custom.coef.map = list(
    "evacuation_more" = "Increased Mobility",
    "evacuation_intra_more" = "Increased Mobility within Cities",
    "evacuation_inter_more" = "Increased Mobility between Cities",
    "evacuation_less" = "Decreased Mobility",
    "evacuation_intra_less" = "Decreased Mobility within Cities",
    "evacuation_inter_less" = "Decreased Mobility between Cities",
    "avg_rainfall" = "Rainfall",
    "burn_rate_int" = "% Average Acres Burned",
    "positivity_rate_lag2" = "Outcome 2 weeks prior",
    "case_rate_lag2" = "Outcome 2 weeks prior",
    "workplaces_int" = "Average Google Mobility to Workplaces",
    "bonding" = "Bonding Social Capital",
    "bridging" = "Bridging Social Capital",
    "linking" = "Linking Social Capital",
    "svi_socioeconomic" = "Socioeconomic Status",
    "svi_household_disability" = "Household Composition & Disability",
    "svi_minority" = "Minority Status & Language",
    "svi_housing_tranport" = "Housing Type & Transportation",
    "health_care_capacity_int" = "Health Care Capacity Index",
    "health_quality_int" = "Health Care Quality Index",
    "employee_muni_int" = "Government Capacity",
    "democrat_2016_int" = "(%) Democrat",
    "(Intercept)" = "Constant"),
  include.fstat = TRUE, include.rsquared = TRUE, 
  include.adjrs = TRUE, include.bic = FALSE,
  include.groups = TRUE, include.aic = FALSE,
  single.row = TRUE,
    custom.gof.names = c(
    rep(NA, 3),
    "s_idios" = "Subdivision Variance", 
    "s_time" = "Temporal Variance")
)


remove(m1,m2,m3,m4,m5,m6,       m7,m8,m9,m10,m11,m12)
```




#### DiD Disaster Damage

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_disaster_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame() %>%
  mutate(week = factor(week))


m1 <- la %>%
  zelig(formula = log(rate5) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(rate5) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, 
      model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(rate5) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_disaster_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(rate5) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(rate5) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(rate5) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_disaster_5wklater.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Disaster Damage",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 608)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 32 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 437)<br>before and after the Glass Fire in Central California<br>among 23 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A5-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A5-2</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A5-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A5-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A5-5</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A5-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:11,
    "<b>Disaster Damage</b>" = 12:15,
    "<b>Time Variant Controls</b>" = 16:17,
    "<b>Social Capital Indices</b>" = 18:20,
    "<b>Social Vulnerability Indices</b>" = 21:24,
    "<b>Health Care & Quality</b>" = 25:26,
    "<b>Governance</b>" = 27:28),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Tracts",
  "evacuation_intra_more:counter" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Tracts",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Tracts",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Tracts",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```

#### DiD Evacuation

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_evac_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()


m1 <- la %>%
  zelig(formula = log(rate5) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(rate5) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(rate5) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_evac_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(rate5) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(rate5) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(rate5) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_evac_5wklater.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Evacuation",
  caption.above = TRUE,
  custom.header = list("Test Positivity Rates (n = 1026)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 54 County Subdivisions" = 1:3, 
                       "Case Rates per 100,000 residents (n = 969)<br>before and after the Glass Fire in Central California<br>among 51 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A6-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A6-2</i>",
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A6-3</i>)",
    
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A6-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A6-5</i>",
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A6-6</i>)"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All Models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:12,
    "<b>Disaster Damage</b>" = 13:16,
    "<b>Time Variant Controls</b>" = 17:18,
    "<b>Social Capital Indices</b>" = 19:21,
    "<b>Social Vulnerability Indices</b>" = 22:25,
    "<b>Health Care & Quality</b>" = 26:27,
    "<b>Governance</b>" = 28:30),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Tracts",
  "counter:evacuation_intra_more" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Tracts",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Tracts",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Tracts",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```

#### DiD Shelter-in-Place

```{r}
library(Zelig)

# Grab cases post-October
la <- read_rds("raw_data/la_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(contains("positivity_rate"), rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    avg_rainfall, 
    positivity_rate_lag2,
    bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
    svi_minority, svi_housing_tranport, workplaces_int,
    health_care_capacity_int, health_quality_int, 
    employee_muni_int, democrat_2016_int),
    funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/la_shelter_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()


m1 <- la %>%
  zelig(formula = log(rate5) ~ 
       positivity_rate_lag2 + counter +
         evacuation_more * counter + 
         avg_rainfall * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         svi_minority + svi_housing_tranport +
         workplaces_int + health_care_capacity_int + health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, 
       model = "ls", weights = "weights")

m2 <- la %>%
  zelig(formula = log(rate5) ~ 
       positivity_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int +health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m3 <- la %>%
  zelig(formula = log(rate5) ~ 
       positivity_rate_lag2 + counter +
        evacuation_intra_more * counter +
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       avg_rainfall * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      svi_minority + svi_housing_tranport +
      workplaces_int + health_care_capacity_int + health_quality_int + 
      employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")


# Grab cases post-October
ca <- read_rds("raw_data/ca_dataset.rds") %>%
    # Zoom into just cases with valid reports
  filter(!is.na(rate5)) %>%
  
  # Fill in zeros for positivity rate with the next smallest value, divided by half
  # this is a good way to deal with a 0 rate, 
  # which is probably not accurate - they probably have COVID,
  # just low levels of it.
  mutate_at(vars(case_rate, case_rate_lag2, rate5), 
            funs(if_else(
    condition = . == 0,
    true = sort(unique(.))[2] / 2,
    false = .))) %>%
  # Simpliy variable to reduce colinearity
  mutate(health_care_capacity_int = ntile(health_care_capacity_int, 4),
         health_quality_int = ntile(health_quality_int, 4),
         svi_socioeconomic = ntile(svi_socioeconomic, 4),
         svi_minority = ntile(svi_minority, 4)) %>%
  # Rescale other predictors 
  mutate_at(vars(
    contains("evacuation"),
    case_rate_lag2,
    burn_rate_int, bonding, bridging, linking, 
    svi_socioeconomic, svi_household_disability, 
      svi_minority, svi_housing_tranport, workplaces_int,
      health_care_capacity_int, health_quality_int, 
      employee_muni_int, democrat_2016_int),
      funs(scale(.))) %>%
    # Join in Coarsened Exact Matching weights, and zoom into key sample
  left_join(by = c("csub"), y = read_rds("raw_data/ca_shelter_match.rds")) %>%
  filter(weights > 0) %>%
  as.data.frame()

m4 <- ca %>%
  zelig(formula = log(rate5) ~ 
       case_rate_lag2 + counter +
         evacuation_more * counter +
         burn_rate_int * counter +
         bonding + bridging + linking + 
         svi_socioeconomic + svi_household_disability + 
         #svi_minority + 
         svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m5 <- ca %>%
  zelig(formula = log(rate5) ~ 
       case_rate_lag2 + counter +
        evacuation_more * counter +
         evacuation_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

m6 <- ca %>%
  zelig(formula = log(rate5) ~ 
       case_rate_lag2 + counter +
        evacuation_intra_more * counter + 
       evacuation_inter_more * counter +
        evacuation_intra_less * counter + 
       evacuation_inter_less * counter +
       burn_rate_int * counter +
      bonding + bridging + linking + 
      svi_socioeconomic + svi_household_disability + 
      #svi_minority + 
        svi_housing_tranport +
         workplaces_int + health_care_capacity_int + #health_quality_int + 
         employee_muni_int + democrat_2016_int - counter + week, model = "ls", weights = "weights")

texreg::htmlreg(
  list(m1,m2, m3, m4,m5,m6),
  single.row = TRUE,
  bold = 0.10,
  stars = c(0.001, 0.01, 0.05, 0.10),
  file = "viz/table_2_shelter_5wklater.html",
  caption = "<b>Matched OLS Difference in Differences Models of COVID-19 Spread Rates (log-transformed) among County Subdivisions</b><br><i>over 19 weeks between August 17 to December 23, 2020</i><br>Matched to test the effect of Shelter-in-Place Behavior",
  caption.above = TRUE,
  custom.header = list(
    "Test Positivity Rates (n = 1064)<br>before and after Hurricane Zeta in Southeastern Louisiana<br>among 56 County Subdivisions" = 1:3, 
    "Case Rates per 100,000 residents (n = 1007)<br>before and after the Glass Fire in Central California<br>among 53 County Subdivisions" = 4:6),
  custom.model.names = c(
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A7-1</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A7-2</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A7-3</i>",
    "1 Type of Movement<br>Evacuation<br>(Fixed Effects)<br><i>Model A7-4</i>", 
    "2 Types of Movement<br>Increases & Decreases<br>(Fixed Effects)<br><i>Model A7-5</i>", 
    "4 Types of Movement<br>Within & Between Tracts<br>(Fixed Effects)<br><i>Model A7-6</i>"),
  custom.note = "*** p < 0.001, ** p < 0.01, * p < 0.05, . p < 0.10. All predictors rescaled to allow for comparison between beta coefficients, interpreted as the expected increase in the log of COVID spread rates given a one standard deviation increase in the predictor. All time-variant measures lagged by 2 weeks, and Google mobility represents average mobility between 2 to 11 days prior to the date in questions. These lags better account for the incubation period of COVID-19. All models include Weekly Fixed Effects.",
  groups = list(
    "<b>Evacuation (per 1000 residents)</b>" = 1:11,
    "<b>Disaster Damage</b>" = 12:15,
    "<b>Time Variant Controls</b>" = 16:17,
    "<b>Social Capital Indices</b>" = 18:20,
    "<b>Social Vulnerability Indices</b>" = 21:24,
    "<b>Health Care & Quality</b>" = 25:26,
    "<b>Governance</b>" = 27:28),
custom.coef.map = list(
  "evacuation_more" = "Increased Mobility",
  "counter:evacuation_more" = "Increased Mobility x Weeks",
  "evacuation_intra_more" = "Increased Mobility within Cities",
  "evacuation_intra_more:counter" = "Increased Mobility within Cities x Weeks",
  "evacuation_inter_more" = "Increased Mobility between Cities",
  "counter:evacuation_inter_more" = "Increased Mobility between Cities x Weeks",
  "evacuation_less" = "Decreased Mobility",
  "counter:evacuation_less" = "Decreased Mobility x Weeks",
  "evacuation_intra_less" = "Decreased Mobility within Cities",
  "counter:evacuation_intra_less" = "Decreased Mobility within Cities x Weeks",
  "evacuation_inter_less" = "Decreased Mobility between Cities",
  "counter:evacuation_inter_less" = "Decreased Mobility between Cities x Weeks",
  "avg_rainfall" = "Rainfall",
  "counter:avg_rainfall" = "Rainfall x Weeks",
  "burn_rate_int" = "% Average Acres Burned",
  "counter:burn_rate_int" = "% Average Acres Burned x Weeks",
  "case_rate_lag2" = "Outcome 2 weeks prior",
  "positivity_rate_lag2" = "Outcome 2 weeks prior",
  "case_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "positivity_rate_lag2:counter" = "Outcome 2 weeks prior x Weeks",
  "workplaces_int" = "Average Google Mobility to Workplaces",
  "bonding" = "Bonding Social Capital",
  "bridging" = "Bridging Social Capital",
  "linking" = "Linking Social Capital",
  "svi_socioeconomic" = "Socioeconomic Status",
  "svi_household_disability" = "Household Composition & Disability",
  "svi_minority" = "Minority Status & Language",
  "svi_housing_tranport" = "Housing Type & Transportation",
  "health_care_capacity_int" = "Health Care Capacity Index",
  "health_quality_int" = "Health Care Quality Index",
  "employee_muni_int" = "Government Capacity",
  "democrat_2016_int" = "(%) Democrat",
  "(Intercept)" = "Constant"),
custom.gof.rows = list(
  "Mean VIF" = list(m1,m2,m3,m4,m5,m6) %>%
    map(~car::vif(from_zelig_model(.)) %>% mean()) %>%
    unlist()),
include.fstat = TRUE, include.rsquared = TRUE, 
include.adjrs = TRUE, include.bic = FALSE,
include.groups = FALSE, include.aic = FALSE)

remove(m1,m2,m3,m4,m5,m6)
 
```

